%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This MATLAB code generates the graphs for the preview of the platform 
% choice extension of "Process or Candidate: The International Community 
% and the Demand for Electoral Integrity". 
%
% DISCLAIMER: Some parts of this code (the election war scenarios) will 
% only produce correct results for the paramter choices of the paper.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%n=11
n=101

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Scenario of an electoral hegemon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solving the FOCS using the symbolic toolbx
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Set symbolic parameters
syms A_gov B_opp Gamma Lambda chi b p c ; 

%Formula of government vote share
x_gov = chi + c + (sqrt(b)-p).*(sqrt(b)-c);

%Formula of resulting bias
beta_p = b-sqrt(b).*p;

%Utility of the international power
u_int = Gamma.*(x_gov.*A_gov + (1-x_gov).*B_opp) - Lambda.*beta_p - c.^2 - p.^2;

%2 FOCS

dc_u_int = diff(u_int,c);
dp_u_int = diff(u_int,p);

%Solve the FOCS

S = solve(dc_u_int==0,dp_u_int==0,c,p);

%Compute other outcomes
S.x_gov = chi + S.c + (sqrt(b)-S.p).*(sqrt(b)-S.c);
S.beta_p = b-sqrt(b).*S.p;


%Create a Matlab function from the solution of the FOCs and include
%outcomes
eq_electoral_hegemon = matlabFunction(S.c,S.p,S.x_gov,S.beta_p,'Vars', [A_gov B_opp Gamma Lambda chi b]);

%%%BACK TO OPTIMIZATION PROBLEM

syms A_gov B_opp Gamma Lambda chi b; 
syms A_gov_bliss B_opp_bliss gamma_gov gamma_opp

%Utility of government

u_gov = S.x_gov - gamma_gov.*(A_gov_bliss - A_gov).^2;

%FOC of government

dA_gov_u_gov = diff(u_gov,A_gov);

%Utility of opposition

u_opp = (1-S.x_gov) - gamma_opp.*(B_opp_bliss - B_opp).^2;

%FOC of opposition

dB_opp_u_opp = diff(u_opp,B_opp);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SYMMETRIC CASES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

chi_double=.5; %incumbent vote share
b_double =0.15; %initial bias
Gamma_double=.75; %importance of country
Lambda_double=.5; %importance of liberalism
gamma_gov_double=.25; %importance of platform of government
gamma_opp_double=.25; %importance of platform of opposition
A_gov_bliss_double=.25; %bliss point of government
B_opp_bliss_double=-.125; %bliss point of opposition

cstar=ones(1,n);
pstar=ones(1,n);
x_gov_star=ones(1,n);
beta_p_star=ones(1,n);
A_gov_star =ones(1,n);
B_opp_star =ones(1,n);

B_opp_bliss_values=linspace(-0.25,0.25,n); %vector of bliss points of opposition

for i=1:n
B_opp_bliss_double=B_opp_bliss_values(i);

dA_gov_u_gov_plugin = subs(dA_gov_u_gov, {Gamma, Lambda, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, {Gamma_double, Lambda_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});
dB_opp_u_opp_plugin = subs(dB_opp_u_opp, {Gamma, Lambda, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, {Gamma_double, Lambda_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});

firststage = solve(dA_gov_u_gov_plugin==0,dB_opp_u_opp_plugin==0,A_gov,B_opp);

A_gov_star(i) =double(firststage.A_gov);
B_opp_star(i) =double(firststage.B_opp);
[cstar(i),pstar(i),x_gov_star(i),beta_p_star(i)] = eq_electoral_hegemon(A_gov_star(i),B_opp_star(i),Gamma_double,Lambda_double,chi_double,b_double);
end

pistar=A_gov_star - B_opp_star;
pi_bliss = A_gov_bliss_double-B_opp_bliss_values;

%Plot the platform choices and fill

plot(B_opp_bliss_values,A_gov_star,'k',B_opp_bliss_values,A_gov_bliss_double*ones(1,n),'--k')
hold on
plot(B_opp_bliss_values,B_opp_star,':r',B_opp_bliss_values,B_opp_bliss_values,'-.r')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[A_gov_star fliplr(A_gov_bliss_double*ones(1,n))]
h1=fill(X,Y,'k')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[B_opp_star fliplr(B_opp_bliss_values)]
h2=fill(X,Y,'r')
hold off

set(h1,'FaceAlpha',.15)
set(h1,'EdgeAlpha',.0)
set(h2,'FaceAlpha',.15)
set(h2,'EdgeAlpha',.0)
set(gca,'YLim',[-0.5 1])

legend({'$$A_{gov}$$','$$\overline{A}_{gov}$$','$$B_{opp}$$','$$\overline{B}_{opp}$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex') 
ylabel('Platform Choice','Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 16a
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Save plot
saveas(gcf,'matlab_online_appendix/figure16a.png')

% JUST TO CHECK THE VOTESHARE

plot(B_opp_bliss_values,x_gov_star,B_opp_bliss_values,beta_p_star,'--')
set(gca,'YLim',[0 1]) 
title('Vote share and resulting bias','Interpreter','latex')
legend({'$$x_{gov}(c^*,p^*)$$','$$b-\sqrt{b}p^*$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%ASYMMETRIC CASE and changes in B_opp*

chi_double=.5; %incumbent vote share
b_double =0.15; %initial bias
Gamma_double=.75; %importance of country
Lambda_double=.5; %importance of liberalism
gamma_gov_double=.25; %importance of platform of government
gamma_opp_double=.2; %importance of platform of opposition
A_gov_bliss_double=.25; %bliss point of government
%B_opp_bliss_double=-.125; %bliss point of opposition

cstar=ones(1,n);
pstar=ones(1,n);
x_gov_star=ones(1,n);
beta_p_star=ones(1,n);
A_gov_star =ones(1,n);
B_opp_star =ones(1,n);

B_opp_bliss_values=linspace(-0.25,0.25,n); %vector of bliss points of opposition

for i=1:n
B_opp_bliss_double=B_opp_bliss_values(i);

dA_gov_u_gov_plugin = subs(dA_gov_u_gov, {Gamma, Lambda, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, {Gamma_double, Lambda_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});
dB_opp_u_opp_plugin = subs(dB_opp_u_opp, {Gamma, Lambda, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, {Gamma_double, Lambda_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});

firststage = solve(dA_gov_u_gov_plugin==0,dB_opp_u_opp_plugin==0,A_gov,B_opp);

A_gov_star(i) =double(firststage.A_gov(2));
B_opp_star(i) =double(firststage.B_opp(2));
[cstar(i),pstar(i),x_gov_star(i),beta_p_star(i)] = eq_electoral_hegemon(A_gov_star(i),B_opp_star(i),Gamma_double,Lambda_double,chi_double,b_double);
end

pistar=A_gov_star - B_opp_star;
pi_bliss = A_gov_bliss_double-B_opp_bliss_values;

%Plot the platform choices and fill

plot(B_opp_bliss_values,A_gov_star,'k',B_opp_bliss_values,A_gov_bliss_double*ones(1,n),'--k')
hold on
plot(B_opp_bliss_values,B_opp_star,':r',B_opp_bliss_values,B_opp_bliss_values,'-.r')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[A_gov_star fliplr(A_gov_bliss_double*ones(1,n))]
h1=fill(X,Y,'k')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[B_opp_star fliplr(B_opp_bliss_values)]
h2=fill(X,Y,'r')
hold off

set(h1,'FaceAlpha',.15)
set(h1,'EdgeAlpha',.0)
set(h2,'FaceAlpha',.15)
set(h2,'EdgeAlpha',.0)

set(gca,'YLim',[-0.5 1])
legend({'$$A_{gov}$$','$$\overline{A}_{gov}$$','$$B_{opp}$$','$$\overline{B}_{opp}$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex') 
ylabel('Platform Choice','Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 17a
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Save plot
saveas(gcf,'matlab_online_appendix/figure17a.png')

% JUST TO CHECK THE VOTESHARE

plot(B_opp_bliss_values,x_gov_star,B_opp_bliss_values,beta_p_star,'--')
set(gca,'YLim',[0 1]) 
title('Vote share and resulting bias','Interpreter','latex')
legend({'$$x_{gov}(c^*,p^*)$$','$$b-\sqrt{b}p^*$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%ASYMMETRIC CASE and changes in gamma_opp

cstar=ones(1,n);
pstar=ones(1,n);
x_gov_star=ones(1,n);
beta_p_star=ones(1,n);
A_gov_star =ones(1,n);
B_opp_star =ones(1,n);

%loop over asymmetric case

gamma_gov_double=.25; %importance of platform of government
gamma_opp_double_values=linspace(.15,.35,n); %importance of platform of opposition
A_gov_bliss_double=.25; %bliss point of government
B_opp_bliss_double=-.125; %bliss point of opposition

for i=1:n
gamma_opp_double=gamma_opp_double_values(i);

dA_gov_u_gov_plugin = subs(dA_gov_u_gov, {Gamma, Lambda, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, {Gamma_double, Lambda_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});
dB_opp_u_opp_plugin = subs(dB_opp_u_opp, {Gamma, Lambda, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, {Gamma_double, Lambda_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});

firststage = solve(dA_gov_u_gov_plugin==0,dB_opp_u_opp_plugin==0,A_gov,B_opp);

if length(firststage.A_gov) == 7
    A_gov_star(i) =double(firststage.A_gov(2));
    B_opp_star(i) =double(firststage.B_opp(2));
elseif length(firststage.A_gov) == 1
    A_gov_star(i) =double(firststage.A_gov);
    B_opp_star(i) =double(firststage.B_opp);
else 
    break
end

[cstar(i),pstar(i),x_gov_star(i),beta_p_star(i)] = eq_electoral_hegemon(A_gov_star(i),B_opp_star(i),Gamma_double,Lambda_double,chi_double,b_double);

end

pistar=A_gov_star - B_opp_star.*ones(1,n);
pi_bliss = (A_gov_bliss_double-B_opp_bliss_double).*ones(1,n);

%Plot the platform choices and fill

plot(gamma_opp_double_values,A_gov_star,'k',gamma_opp_double_values,A_gov_bliss_double*ones(1,n),'--k')
hold on
plot(gamma_opp_double_values,B_opp_star,':r',gamma_opp_double_values,B_opp_bliss_double.*ones(1,n),'-.r')
X=[gamma_opp_double_values fliplr(gamma_opp_double_values)]
Y=[A_gov_star fliplr(A_gov_bliss_double*ones(1,n))]
h1=fill(X,Y,'k')
X=[gamma_opp_double_values fliplr(gamma_opp_double_values)]
Y=[B_opp_star fliplr(B_opp_bliss_double.*ones(1,n))]
h2=fill(X,Y,'r')
hold off

set(h1,'FaceAlpha',.15)
set(h1,'EdgeAlpha',.0)
set(h2,'FaceAlpha',.15)
set(h2,'EdgeAlpha',.0)

set(gca,'XLim',[0.15 0.35])
set(gca,'XTick',[0.2 0.25 0.3]);
set(gca,'YLim',[-0.5 1])
legend({'$$A_{gov}$$','$$\overline{A}_{gov}$$','$$B_{opp}$$','$$\overline{B}_{opp}$$'},'Location','northeast','Interpreter','latex')
xlabel({'$$\gamma_{opp}$$'},'Interpreter','latex') 
ylabel('Platform Choice','Interpreter','latex') 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 16a
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Save plot
saveas(gcf,'matlab_online_appendix/figure16a.png')

plot(gamma_opp_double_values,x_gov_star,gamma_opp_double_values,beta_p_star,'--')
set(gca,'YLim',[0 1]) 
title('Vote share and resulting bias','Interpreter','latex')
legend({'$$x_{gov}(c^*,p^*)$$','$$b-\sqrt{b}p^*$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solving the FOCS using the symbolic toolbx
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

syms A_gov B_opp Gammaplus Gammaminus Lambdaplus Lambdaminus b pplus pminus cplus cminus chi

x_gov = chi + cplus +cminus + (sqrt(b)-(pplus+pminus)).*(sqrt(b)-cplus-cminus);
beta_p = b-sqrt(b).*(pplus+pminus);

u_plus = Gammaplus.*(x_gov.*A_gov + (1-x_gov).*B_opp)-Lambdaplus.*beta_p-cplus^2-pplus^2;
u_minus = -Gammaminus.*(x_gov.*A_gov + (1-x_gov).*B_opp)-Lambdaminus.*beta_p-cminus^2-pminus^2;

%%%%

dpuplus = diff(u_plus,pplus);
dcuplus = diff(u_plus,cplus);

%%%%

dpuminus = diff(u_minus,pminus);
dcuminus = diff(u_minus,cminus);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% General Case: Solve the system of 4 equations in 4 variables and save
% solution in Matlab function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Create a system of algebraic equations from first order conditions

S = solve(dpuplus==0,dcuplus==0, dpuminus==0,dcuminus==0,cplus,cminus,pplus,pminus);

%Convert the symbolic expressions to strings for use in graphs + numerical
%simulations

%Compute other outcomes
S.x_gov = chi + (S.cplus+S.cminus) + (sqrt(b)-(S.pplus+S.pminus)).*(sqrt(b)-(S.cplus+S.cminus));
S.beta_p = b-sqrt(b).*(S.pplus+S.pminus);

eq_election_war = matlabFunction(S.cplus,S.cminus,S.pplus,S.pminus,S.x_gov, S.beta_p,'Vars', [A_gov B_opp Gammaplus Lambdaplus Gammaminus Lambdaminus chi b])

%%%BACK TO OPTIMIZATION PROBLEM

syms A_gov B_opp Gammaplus Gammaminus Lambdaplus Lambdaminus b pplus pminus cplus cminus chi
syms A_gov_bliss B_opp_bliss gamma_gov gamma_opp

%Utility of government

u_gov = S.x_gov - gamma_gov.*(A_gov_bliss - A_gov).^2;

%FOC of government

dA_gov_u_gov = diff(u_gov,A_gov);

%Utility of opposition

u_opp = (1-S.x_gov) - gamma_opp.*(B_opp_bliss - B_opp).^2;

%FOC of opposition

dB_opp_u_opp = diff(u_opp,B_opp);

%Make a clean version of replace 

%SYMMETRIC CASES

chi_double=.5; %incumbent vote share
b_double =0.15; %initial bias
Gammaplus_double=.75; %importance of country
Lambdaplus_double=.5; %importance of liberalism
Gammaminus_double=.5; %importance of country
Lambdaminus_double=.0; %importance of liberalism

gamma_gov_double=.25; %importance of platform of government
gamma_opp_double=.25; %importance of platform of opposition
A_gov_bliss_double=.25; %bliss point of government
B_opp_bliss_double=-.125; %bliss point of opposition

cstar=ones(1,n);
pstar=ones(1,n);
x_gov_star=ones(1,n);
beta_p_star=ones(1,n);
A_gov_star =ones(1,n);
B_opp_star =ones(1,n);

B_opp_bliss_values=linspace(-.25,0.25,n); %vector of bliss points of opposition

for i=1:n
B_opp_bliss_double=B_opp_bliss_values(i);

dA_gov_u_gov_plugin = subs(dA_gov_u_gov, ...
    {Gammaplus, Gammaminus, Lambdaplus, Lambdaminus, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, ...
    {Gammaplus_double, Gammaminus_double, Lambdaplus_double, Lambdaminus_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});
dB_opp_u_opp_plugin = subs(dB_opp_u_opp, ...
    {Gammaplus, Gammaminus, Lambdaplus, Lambdaminus, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, ...
    {Gammaplus_double, Gammaminus_double, Lambdaplus_double, Lambdaminus_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});

firststage = solve(dA_gov_u_gov_plugin==0,dB_opp_u_opp_plugin==0,A_gov,B_opp);

if length(firststage.A_gov) == 7
    A_gov_star(i) =double(firststage.A_gov(2));
    B_opp_star(i) =double(firststage.B_opp(2));
elseif length(firststage.A_gov) == 1
    A_gov_star(i) =double(firststage.A_gov);
    B_opp_star(i) =double(firststage.B_opp);
else 
    break
end


%TBD: evenutally if-statement for case of single solution

[cplusstar(i),cminusstar(i),pplusstar(i),pminusstar(i),x_gov_star(i),beta_p_star(i)] = eq_election_war(A_gov_star(i),B_opp_star(i),Gammaplus_double,Lambdaplus_double,Gammaminus_double,Lambdaminus_double,chi_double,b_double);
end

pistar=A_gov_star - B_opp_star;
pi_bliss = A_gov_bliss_double-B_opp_bliss_values;

%Plot the platform choices and fill

plot(B_opp_bliss_values,A_gov_star,'k',B_opp_bliss_values,A_gov_bliss_double*ones(1,n),'--k')
hold on
plot(B_opp_bliss_values,B_opp_star,':r',B_opp_bliss_values,B_opp_bliss_values,'-.r')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[A_gov_star fliplr(A_gov_bliss_double*ones(1,n))]
h1=fill(X,Y,'k')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[B_opp_star fliplr(B_opp_bliss_values)]
h2=fill(X,Y,'r')
hold off

set(h1,'FaceAlpha',.15)
set(h1,'EdgeAlpha',.0)
set(h2,'FaceAlpha',.15)
set(h2,'EdgeAlpha',.0)

set(gca,'YLim',[-0.5 1])
legend({'$$A_{gov}$$','$$\overline{A}_{gov}$$','$$B_{opp}$$','$$\overline{B}_{opp}$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex') 
ylabel('Platform Choice','Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 16b
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Save plot
saveas(gcf,'matlab_online_appendix/figure16b.png')

% JUST TO CHECK THE VOTESHARE

plot(B_opp_bliss_values,x_gov_star,B_opp_bliss_values,beta_p_star,'--')
set(gca,'YLim',[0 1]) 
title('Vote share and resulting bias','Interpreter','latex')
legend({'$$x_{gov}(c^*,p^*)$$','$$b-\sqrt{b}p^*$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%ASYMMETRIC CASE and changes in B_opp*

chi_double=.5; %incumbent vote share
b_double =0.15; %initial bias
Gamma_double=.75; %importance of country
Lambda_double=.5; %importance of liberalism
gamma_gov_double=.25; %importance of platform of government
gamma_opp_double=.2; %importance of platform of opposition
A_gov_bliss_double=.25; %bliss point of government

cstar=ones(1,n);
pstar=ones(1,n);
x_gov_star=ones(1,n);
beta_p_star=ones(1,n);
A_gov_star =ones(1,n);
B_opp_star =ones(1,n);

B_opp_bliss_values=linspace(-.25,0.25,n); %vector of bliss points of opposition

for i=1:n
B_opp_bliss_double=B_opp_bliss_values(i);

dA_gov_u_gov_plugin = subs(dA_gov_u_gov, ...
    {Gammaplus, Gammaminus, Lambdaplus, Lambdaminus, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, ...
    {Gammaplus_double, Gammaminus_double, Lambdaplus_double, Lambdaminus_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});
dB_opp_u_opp_plugin = subs(dB_opp_u_opp, ...
    {Gammaplus, Gammaminus, Lambdaplus, Lambdaminus, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, ...
    {Gammaplus_double, Gammaminus_double, Lambdaplus_double, Lambdaminus_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});

firststage = solve(dA_gov_u_gov_plugin==0,dB_opp_u_opp_plugin==0,A_gov,B_opp);

if length(firststage.A_gov) == 7
    A_gov_star(i) =double(firststage.A_gov(2));
    B_opp_star(i) =double(firststage.B_opp(2));
elseif length(firststage.A_gov) == 1
    A_gov_star(i) =double(firststage.A_gov);
    B_opp_star(i) =double(firststage.B_opp);
else 
    break
end

[cplusstar(i),cminusstar(i),pplusstar(i),pminusstar(i),x_gov_star(i),beta_p_star(i)] = eq_election_war(A_gov_star(i),B_opp_star(i),Gammaplus_double,Lambdaplus_double,Gammaminus_double,Lambdaminus_double,chi_double,b_double);
end

pistar=A_gov_star - B_opp_star.*ones(1,n);
pi_bliss = (A_gov_bliss_double-B_opp_bliss_values);

%Plot the platform choices and fill
plot(B_opp_bliss_values,A_gov_star,'k',B_opp_bliss_values,A_gov_bliss_double*ones(1,n),'--k')
hold on
plot(B_opp_bliss_values,B_opp_star,':r',B_opp_bliss_values,B_opp_bliss_values,'-.r')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[A_gov_star fliplr(A_gov_bliss_double*ones(1,n))]
h1=fill(X,Y,'k')
X=[B_opp_bliss_values fliplr(B_opp_bliss_values)]
Y=[B_opp_star fliplr(B_opp_bliss_values)]
h2=fill(X,Y,'r')
hold off

set(h1,'FaceAlpha',.15)
set(h1,'EdgeAlpha',.0)
set(h2,'FaceAlpha',.15)
set(h2,'EdgeAlpha',.0)

set(gca,'YLim',[-0.5 1])
legend({'$$A_{gov}$$','$$\overline{A}_{gov}$$','$$B_{opp}$$','$$\overline{B}_{opp}$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex') 
ylabel('Platform Choice','Interpreter','latex') 

%Save plot
saveas(gcf,'matlab_online_appendix/figure17b.png')

% JUST TO CHECK THE VOTESHARE

plot(B_opp_bliss_values,x_gov_star,B_opp_bliss_values,beta_p_star,'--')
set(gca,'YLim',[0 1]) 
title('Vote share and resulting bias','Interpreter','latex')
legend({'$$x_{gov}(c^*,p^*)$$','$$b-\sqrt{b}p^*$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%ASYMMETRIC CASES and gamma

cplusstar=ones(1,n);
pplusstar=ones(1,n);
cminusstar=ones(1,n);
pminusstar=ones(1,n);
x_gov_star=ones(1,n);
beta_p_star=ones(1,n);
A_gov_star =ones(1,n);
B_opp_star =ones(1,n);

%loop over asymmetric case

gamma_gov_double=.25; %importance of platform of government
gamma_opp_double_values=linspace(.15,.35,n); %importance of platform of opposition
A_gov_bliss_double=.25; %bliss point of government
B_opp_bliss_double=-.125; %bliss point of opposition

for i=1:n
gamma_opp_double=gamma_opp_double_values(i);

dA_gov_u_gov_plugin = subs(dA_gov_u_gov, ...
    {Gammaplus, Gammaminus, Lambdaplus, Lambdaminus, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, ...
    {Gammaplus_double, Gammaminus_double, Lambdaplus_double, Lambdaminus_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});
dB_opp_u_opp_plugin = subs(dB_opp_u_opp, ...
    {Gammaplus, Gammaminus, Lambdaplus, Lambdaminus, chi, b, A_gov_bliss, B_opp_bliss, gamma_gov, gamma_opp}, ...
    {Gammaplus_double, Gammaminus_double, Lambdaplus_double, Lambdaminus_double, chi_double, b_double, A_gov_bliss_double, B_opp_bliss_double, gamma_gov_double, gamma_opp_double});

firststage = solve(dA_gov_u_gov_plugin==0,dB_opp_u_opp_plugin==0,A_gov,B_opp);

if length(firststage.A_gov) == 7
    A_gov_star(i) =double(firststage.A_gov(2));
    B_opp_star(i) =double(firststage.B_opp(2));
elseif length(firststage.A_gov) == 1
    A_gov_star(i) =double(firststage.A_gov);
    B_opp_star(i) =double(firststage.B_opp);
else 
    break
end


%TBD: evenutally if-statement for case of single solution

[cplusstar(i),cminusstar(i),pplusstar(i),pminusstar(i),x_gov_star(i),beta_p_star(i)] = eq_election_war(A_gov_star(i),B_opp_star(i),Gammaplus_double,Lambdaplus_double,Gammaminus_double,Lambdaminus_double,chi_double,b_double);
end

pistar=A_gov_star - B_opp_star.*ones(1,n);
pi_bliss = (A_gov_bliss_double-B_opp_bliss_double).*ones(1,n);

x_gov_star
beta_p_star

%Plot the platform choices and fill
plot(gamma_opp_double_values,A_gov_star,'k',gamma_opp_double_values,A_gov_bliss_double*ones(1,n),'--k')
hold on
plot(gamma_opp_double_values,B_opp_star,':r',gamma_opp_double_values,B_opp_bliss_double.*ones(1,n),'-.r')
X=[gamma_opp_double_values fliplr(gamma_opp_double_values)]
Y=[A_gov_star fliplr(A_gov_bliss_double*ones(1,n))]
h1=fill(X,Y,'k')
X=[gamma_opp_double_values fliplr(gamma_opp_double_values)]
Y=[B_opp_star fliplr(B_opp_bliss_double.*ones(1,n))]
h2=fill(X,Y,'r')
hold off

set(h1,'FaceAlpha',.15)
set(h1,'EdgeAlpha',.0)
set(h2,'FaceAlpha',.15)
set(h2,'EdgeAlpha',.0)

set(gca,'XLim',[0.15 0.35])
set(gca,'XTick',[0.2 0.25 0.3]);
set(gca,'YLim',[-0.5 1])

%title('Platform choices by both parties','Interpreter','latex')
legend({'$$A_{gov}$$','$$\overline{A}_{gov}$$','$$B_{opp}$$','$$\overline{B}_{opp}$$'},'Location','northeast','Interpreter','latex')
xlabel({'$$\gamma_{opp}$$'},'Interpreter','latex') 
ylabel('Platform Choice','Interpreter','latex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figure 18
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Save plot
saveas(gcf,'matlab_online_appendix/figure18.png')

% JUST TO CHECK THE VOTESHARE

plot(gamma_opp_double_values,x_gov_star,gamma_opp_double_values,beta_p_star,'--')
set(gca,'YLim',[0 1]) 
title('Vote share and resulting bias','Interpreter','latex')
legend({'$$x_{gov}(c^*,p^*)$$','$$b-\sqrt{b}p^*$$'},'Location','northwest','Interpreter','latex')
xlabel({'$$\overline{B}_{opp}$$'},'Interpreter','latex')
